#include "Box2D.h"

b2World* Box2D::myWorld = NULL;
float32 Box2D::timeStep = 0;
int32 Box2D::velocityIterations = 0;
int32 Box2D::positionIterations = 0;

Box2D::Box2D()
{
}

Box2D::~Box2D()
{
}

void Box2D::InitBox2D()
{
	b2Vec2 gravity(0.0f, 10.0f);
	myWorld = new b2World(gravity);

	timeStep = 1.0f / 60.0f;
	int32 velocityIterations = 6;
	int32 positionIterations = 2;
}

b2Body* Box2D::CreateStaticBody(float x, float y, float h_width, float h_height)
{
	b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
	bodyDef.position.Set(x, y);

	b2Body* staticBody = myWorld->CreateBody(&bodyDef);
	
	b2PolygonShape groundBox;
	groundBox.SetAsBox(h_width, h_height);
	staticBody->CreateFixture(&groundBox, 0.0f);

	return staticBody;
}

b2Body* Box2D::CreateDynamicBody(float x, float y, float h_width, float h_height)
{
	b2BodyDef bodyDef;
	bodyDef.type = b2_dynamicBody;
	bodyDef.position.Set(x, y);
	
	b2Body* body = myWorld->CreateBody(&bodyDef);

	b2PolygonShape dynamicBox;
	dynamicBox.SetAsBox(h_width, h_height);

	b2FixtureDef fixtureDef;
	fixtureDef.shape = &dynamicBox;
	fixtureDef.density = 1.0f;
	fixtureDef.friction = 0.3f;
	
	body->CreateFixture(&fixtureDef);

	return body;
}

void Box2D::Step()
{
	myWorld->Step(timeStep, velocityIterations, positionIterations);
}

